package com.fingard.dsp.bank.directbank.gdrc01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/10/10.
 * 单笔余额、批量余额、历史余额
 */
public class GDRC01Balance extends GDRC01Base implements IBalance{

    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception{
        return d7506BalQry_A(p_req7506);
    }

    /**
     * 集团含成员单位
     * @param p_req7506
     * @return
     * @throws Exception
     */
    private RetAts7506BalQry d7506BalQry_A(Req7506BalQry p_req7506) throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                //拼接请求报文头
                GDRC01Header gdrc01Header=new GDRC01Header();
                gdrc01Header.TrsCode="B2EGroupActBalQry";//交易码
                gdrc01Header.ERPJnlNo=p_req7506.reqHeader.transSeqID;//流水号
                writeCommonHeader(xmlTextWriter,gdrc01Header);
                xmlTextWriter.writeEndElement();
                //拼接请求报文体
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeElementString("AcNo",p_req7506.readedDetail.actNum);
                xmlTextWriter.writeElementString("BalRange","1");
                xmlTextWriter.writeElementString("BeginDate","");
                xmlTextWriter.writeElementString("EndDate","");
                xmlTextWriter.writeElementString("BeginNo","1");
                xmlTextWriter.writeElementString("RecordNum","");
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

                //测试
                //bankResult[0]="";
                //bankResult[1]="<Message>   <Head>      <IBSReturnCode>000000</IBSReturnCode>      <IBSReturnMsg>交易成功</IBSReturnMsg>   </Head>   <Body>      <RecordCount>1</RecordCount>      <TotalCount>1</TotalCount>      <List>         <Map>            <AcNo>80020000000807809</AcNo>            <SerialNo>1</SerialNo>            <AcName>汇海测试账号789456123</AcName>            <DeptId>08351</DeptId>            <DeptName>江门市新会区农村信用合作联社营业部</DeptName>            <Currency>CNY</Currency>            <ProductDesc>单位存款基本账户</ProductDesc>            <Balance>102160.64</Balance>            <BalDate>20181121</BalDate>            <AvailBal>102160.64</AvailBal>            <DCFlag/>            <OverdrawLimit/>            <AcState>00</AcState>            <BusinessDate>20181121</BusinessDate>         </Map>      </List>   </Body></Message>\n";
                //WriteBankLogStep3(bankResult[1]);

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if("000000".equals(respCode)){//交易成功
                            String recordCount = tmpBkReader.getFirstText("/Message/Body/RecordCount");//页面返回记录数
                            if(!"".equals(recordCount) && Integer.parseInt(recordCount)>0){
                                if (tmpBkReader.read("/Message/Body/List/Map")) {
                                    detail.respCode = "0";//响应码
                                    detail.respInfo = "成功";//响应信息
                                    detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("Currency"));//币种
                                    detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("AcNo");//账号
                                    detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBal");//可用余额
                                    detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Balance");//余额
                                    detail.frozenBal="";//冻结余额
                                }
                            } else{
                                detail.respCode = "-1";
                                detail.respInfo = "未查询到记录";
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = respInfo;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e){
            WriteBankLogLn("查询余额异常"+e);
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    /**
     * 集团不含成员单位
     * @param p_req7506
     * @return
     * @throws Exception
     */
    private RetAts7506BalQry d7506BalQry_B(Req7506BalQry p_req7506) throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            while (p_req7506.readRecord()) {
                RetAtsBalDetail detail = new RetAtsBalDetail();
                //非公有参数
                XmlTextWriter xmlTextWriter = new XmlTextWriter();
                //拼接请求报文头
                GDRC01Header gdrc01Header=new GDRC01Header();
                gdrc01Header.TrsCode="B2EActBalQry";//交易码
                gdrc01Header.ERPJnlNo=p_req7506.reqHeader.transSeqID;//流水号
                writeCommonHeader(xmlTextWriter,gdrc01Header);
                xmlTextWriter.writeEndElement();
                //拼接请求报文体
                xmlTextWriter.writeStartElement("Body");
                xmlTextWriter.writeStartElement("List");
                xmlTextWriter.writeStartElement("Map");
                xmlTextWriter.writeElementString("AcNo",p_req7506.readedDetail.actNum);
                xmlTextWriter.writeEndElement();
                xmlTextWriter.writeEndDocument();
                //发送请求报文
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString());

                //测试
                //bankResult[0]="";
                //bankResult[1]="<?xml version=\"1.0\" encoding=\"utf-8\"?><Message>  <Head>    <IBSReturnCode>000000</IBSReturnCode>    <IBSReturnMsg>成功</IBSReturnMsg>    <IBSJnlNo>111111111</IBSJnlNo>  </Head>  <Body>    <List>      <Map>        <ReturnCode>000000</ReturnCode>        <ReturnMsg>成功</ReturnMsg>        <IBSJnlNo/>        <AcNo>10000000000000</AcNo>        <AcName>企业客户12</AcName>        <Currency>CNY</Currency>        <Balance>264427.00</Balance>        <AvailBal>204427.00</AvailBal>        <FreezeBal>10.00</FreezeBal>        <OpenDate/>        <AcState>0</AcState>        <DeptId>06101</DeptId>        <DepositPeriod/>        <RateType/>      </Map>    </List>  </Body></Message>\n";
                //WriteBankLogStep3(bankResult[1]);

                //处理响应报文
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSb = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(bankResult[1], tmpSb);
                    if (tmpBkReader != null) {
                        String respCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");
                        String respInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");
                        if("000000".equals(respCode)){//交易成功
                            if (tmpBkReader.read("/Message/Body/List/Map")) {
                                String returnCode = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnCode");
                                String returnMsg = tmpBkReader.readedXElement.getSingleTextNotNull("ReturnMsg");
                                if("000000".equals(returnCode)){
                                    detail.respCode = "0";//响应码
                                    detail.respInfo = returnMsg;//响应信息
                                    detail.actCur = convertCurToATS(tmpBkReader.readedXElement.getSingleTextNotNull("Currency"));//币种
                                    detail.actNum = tmpBkReader.readedXElement.getSingleTextNotNull("AcNo");//账号
                                    detail.availBal = tmpBkReader.readedXElement.getSingleTextNotNull("AvailBal");//可用余额
                                    detail.currBal = tmpBkReader.readedXElement.getSingleTextNotNull("Balance");//余额
                                    detail.frozenBal=tmpBkReader.readedXElement.getSingleTextNotNull("FreezeBal");//冻结余额
                                }else{
                                    detail.respCode = "-1";
                                    detail.respInfo = returnMsg;
                                }
                            }
                        }else{
                            detail.respCode = "-1";
                            detail.respInfo = respInfo;
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = tmpSb.toString();
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e){
            WriteBankLogLn("查询余额异常"+e);
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception{
        return null;
    }
}
